package com.emotion.recognition.client;

import java.util.Map;
import java.util.Map.Entry;

import com.emotion.recognition.shared.Constants;
import com.emotion.recognition.shared.Emotion;
import com.emotion.recognition.shared.Results;
import com.emotion.recognition.shared.SomResults;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ResultsPanel extends Composite {

    private static ResultsPanelUiBinder uiBinder = GWT.create(ResultsPanelUiBinder.class);

    interface ResultsPanelUiBinder extends UiBinder<Widget, ResultsPanel> {
    }

    @UiField
    HTMLPanel htmlResultsPanel;

    @UiField
    VerticalPanel verticalPanel;

    Image trainingImage;

    public ResultsPanel(Results results) {
        initWidget(uiBinder.createAndBindUi(this));

        showSOMResults(results.somResults);
        showMLPResults(results.mlpResults);
    }

    private void showMLPResults(Map<String, Double[]> mlpResults) {
        for (Map.Entry<String, Double[]> entry : mlpResults.entrySet()) {
            Image image = new Image(Constants.TESTING_DIRECTORY_CLIENT + "/" + entry.getKey());
            image.setStyleName("resultsImage");

            BarChart barChart = new BarChart();
            Double[] output = entry.getValue();
            barChart.setData(output);

            HorizontalPanel panel = new HorizontalPanel();
            panel.add(image);
            panel.add(barChart);
            verticalPanel.add(panel);
        }
    }

    private void showSOMResults(SomResults somResults) {
        Map<String, Integer>[][] somTotalResults = somResults.somTotalResults;
        Map<String, Map<Emotion, Double>> somTestingResults = somResults.somTestingResults;

        // Always have a square grid
        int resultsSize = somTotalResults.length;
        Grid g = new Grid(resultsSize, resultsSize);
        g.addStyleName("somResultsStyle");

        double astonished_max = 0, sad_max = 0, smiling_max = 0, calm_max = 0;
        for (int row = 0; row < resultsSize; ++row) {
            for (int col = 0; col < resultsSize; ++col) {
                Map<String, Integer> map = somTotalResults[row][col];
                for (Entry<String, Integer> entry : map.entrySet()) {
                    if (entry.getKey().equals("ASTONISHED")) {
                        if (entry.getValue() > astonished_max) {
                            astonished_max = entry.getValue();
                        }
                    } else if (entry.getKey().equals("SAD")) {
                        if (entry.getValue() > sad_max) {
                            sad_max = entry.getValue();
                        }
                    } else if (entry.getKey().equals("SMILING")) {
                        if (entry.getValue() > smiling_max) {
                            smiling_max = entry.getValue();
                        }
                    } else if (entry.getKey().equals("CALM")) {
                        if (entry.getValue() > calm_max) {
                            calm_max = entry.getValue();
                        }
                    }
                }
            }
        }

        // Put some values in the grid cells.
        for (int row = 0; row < resultsSize; ++row) {
            for (int col = 0; col < resultsSize; ++col) {
                // g.setText(row, col, "" + row + ", " + col);
                Map<String, Integer> map = somTotalResults[row][col];
                String txt;
                for (Entry<String, Integer> entry : map.entrySet()) {
                    txt = "";
                    if (entry.getKey().equals("ASTONISHED")) {
                        // Training data
                        txt += entry.getKey() + " " + entry.getValue();
                        g.setHTML(
                                row,
                                col,
                                g.getHTML(row, col)
                                        + "<div style='width:100%; padding: 3px 0; background-color:rgba(255,0,0,"
                                        + entry.getValue() / astonished_max * 4 + ");'>" + txt
                                        + "</div>");
                    } else if (entry.getKey().equals("SMILING")) {
                        // Training data
                        txt += entry.getKey() + " " + entry.getValue();
                        g.setHTML(
                                row,
                                col,
                                g.getHTML(row, col)
                                        + "<div style='width:100%; padding: 3px 0; background-color:rgba(0,0,250,"
                                        + entry.getValue() / smiling_max * 4 + ");'>" + txt
                                        + "</div>");
                    } else if (entry.getKey().equals("CALM")) {
                        // Training data
                        txt += entry.getKey() + " " + entry.getValue();
                        g.setHTML(
                                row,
                                col,
                                g.getHTML(row, col)
                                        + "<div style='width:100%; padding: 3px 0; background-color:rgba(35,235,0,"
                                        + entry.getValue() / calm_max * 4 + ");'>" + txt + "</div>");
                    } else if (entry.getKey().equals("SAD")) {
                        // Training data
                        txt += entry.getKey() + " " + entry.getValue();
                        g.setHTML(
                                row,
                                col,
                                g.getHTML(row, col)
                                        + "<div style='width:100%; padding: 3px 0; background-color:rgba(255,255,0,"
                                        + entry.getValue() / sad_max * 4 + ");'>" + txt + "</div>");
                    } else {
                        // Testing data
                        /*
                         * trainingImage = new Image();
                         * trainingImage.setUrl(Constants.TESTING_DIRECTORY_CLIENT + "/" +
                         * entry.getKey()); trainingImage.setSize("80px", "80px"); if
                         * (g.getWidget(row, col) != null) { trainingImage.setSize("100px",
                         * "100px"); } g.setWidget(row, col, trainingImage);
                         * System.out.println(g.getHTML(row, col));
                         */

                        Map<Emotion, Double> fitnessValues = somTestingResults.get(entry.getKey());
                        String title = "";
                        for (Emotion valueEntry : fitnessValues.keySet()) {
                            title += valueEntry + " = " + fitnessValues.get(valueEntry) + "\n";
                        }
                        title = title.substring(0, title.length() - 2);

                        g.setHTML(row, col, g.getHTML(row, col)
                                + "<img class='gwt-Image' src='../images/testing/" + entry.getKey()
                                + "' style='width: 50px; height: 50px; ' title='" + title + "'>");
                    }
                }
            }
        }

        htmlResultsPanel.add(g);
    }
}
